(async function(){
    const doms={
        aside:{
            nickname:document.querySelector('.aside-name'),
            loginid:document.querySelector('.aside-account'),
        },
        close:document.querySelector('.close'),
        chatcontainer:document.querySelector('.chat-container'),
        form:document.querySelector('form'),
        input:document.querySelector('.msg-container input')
    }
    close.onclick = function(){//注销
        lineout();
        location.href='./login.html'
    }
    function lineout(){
        API.loginout();
    }


  //-------------用户验证
        const resp=await API.profile();
        if(resp.code===0){
            doms.aside.nickname.innerText=resp.data.nickname;
            doms.aside.loginid.innerText=resp.data.loginId;
        }
        else{
            alert(resp.msg);
            location.href='./login.html';
            return;
        }
        const user=resp.data;
    //---------获取历史记录
   async function getHistory(){
        const resp=await API.getHistory();
        for(let item of resp.data){//将历史记录添加到页面
            addchart(item);
        }
        changescroll();
    }
    //创建元素并将对话内容加入
    function addchart(item){ 
        const div=document.createElement('div');
            const img=document.createElement('img');
            const content=document.createElement('div');
            const time=document.createElement('div');
            div.classList.add('chat-item');
            img.className="chat-avatar";
            content.className='chat-content';
            content.innerText=item.content;
            time.className='chat-date';
            time.innerText=changetime(item.createdAt);
            if(item.from)
            {
                div.classList.add('me');
                img.src="./asset/avatar.png";
            }
            else
            img.src="./asset/robot-avatar.jpg";
            div.appendChild(img);
            div.appendChild(content);
            div.appendChild(time);
            doms.chatcontainer.appendChild(div);
    }
    function changetime(data){
        const value=new Date(data);
        const year=value.getFullYear();
        const month=(value.getMonth()+1).toString().padStart(2,'0');
        const day=(value.getDate()).toString().padStart(2,'0');
        const hour=(value.getHours()).toString().padStart(2,'0');
        const minuter=(value.getMinutes()).toString().padStart(2,'0');
        const second=(value.getSeconds()).toString().padStart(2,'0');
        return `${year}-${month}-${day} ${hour}:${minuter}:${second}`;
    }

    //将滚动条滚到最下面
    function changescroll(){
        doms.chatcontainer.scrollTop = doms.chatcontainer.scrollHeight;
    }
    getHistory();


    // 发送消息
     doms.form.onsubmit = function(e){
        e.preventDefault();
        sendChart();
        changescroll();
     }
        async function sendChart(){
        const value=doms.input.value.trim();
        if(!value)
        return;
        doms.input.value='';
            addchart({
                from:user.loginId,
                to:null,
                createdAt:Date.now(),
                content:value
            })
          const data=await API.sendChart(value);
          addchart({
            from:null,
            to:user.loginId,
            ...data.data,
          })
          changescroll();
        }
}
)()